import * as STS from 'qcloud-cos-sts'
export const config: any = {
    secretId: process.env.GROUP_SECRET_ID || 'AKIDkT7e8RYQ4VkriMo1zqeHgux0xiIhn27z',   // 固定密钥
    secretKey: process.env.GROUP_SECRET_KEY || 'l6VwERj0FOMG8v24vb7QcM8lEg2lG3mJ',  // 固定密钥
    proxy: '',
    host: 'sts.tencentcloudapi.com', // 域名，非必须，默认为 sts.tencentcloudapi.com
    endpoint: 'sts.internal.tencentcloudapi.com', // 域名，非必须，与host二选一，默认为 sts.tencentcloudapi.com
    durationSeconds: 1800,  // 密钥有效期
    // 放行判断相关参数
    bucket: process.env.COS_BUCKET || 'ztf160-1304097609', // 换成你的 bucket
    region: process.env.COS_REGION || 'ap-guangzhou', // 换成 bucket 所在地区
    allowPrefix: '' // 这里改
}
export class GenerateSecretkey {
    config: any;
    // private shortBucketName = this.config.bucket.subsr(0, this.config.bucket.lastIndexOf('-'));
    private appId;
    constructor(options = {}) {
        this.config = Object.assign(options, config)
        this.appId = this.config.bucket.substring(1 + this.config.bucket.lastIndexOf('-'));
    }
    private getPolicy() {
        const policy = {
            'version': '2.0',
            'statement': [{
              'action': [
                // 简单上传
                'name/cos:PutObject',
                'name/cos:PostObject',
                // 分片上传
                'name/cos:InitiateMultipartUpload',
                'name/cos:ListMultipartUploads',
                'name/cos:ListParts',
                'name/cos:UploadPart',
                'name/cos:CompleteMultipartUpload',
                // 简单上传和分片，需要以上权限，其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
          
                // 文本审核任务
                'name/ci:CreateAuditingTextJob',
                // 开通媒体处理服务
                'name/ci:CreateMediaBucket'
                // 更多数据万象授权可参考：https://cloud.tencent.com/document/product/460/41741
              ],
              'effect': 'allow',
              'principal': { 'qcs': ['*'] },
              'resource': [
                'qcs::cos:' + this.config.region + ':uid/' + this.appId + ':' + this.config.bucket + '/*',
                // ci相关授权，按需使用
                'qcs::ci:' + this.config.region + ':uid/' + this.appId + ':' + this.config.bucket + '/*',
              ],
            }],
          };
          return policy;
    }
    // 获取临时密钥
    public getCredential(callback) {
        const params = {
            secretId: this.config.secretId,
            secretKey: this.config.secretKey,
            proxy: this.config.proxy,
            durationSeconds: this.config.durationSeconds,
            policy: this.getPolicy(),
        }
        const _callback = (err, res) => {
            if(err) callback(err)
            const credentials = {
                TmpSecretId: res.credentials.tmpSecretId,
                TmpSecretKey: res.credentials.tmpSecretKey,
                SecurityToken: res.credentials.sessionToken,
                StartTime: res.startTime,
                ExpiredTime: res.expiredTime
            }
            callback(null, credentials)
        }
        STS.getCredential(params, _callback)
    }
} 